package book.BouncySquare;

import javax.microedition.khronos.egl.EGL10;                                 //1 
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.opengl.GLSurfaceView;                                         //2
import java.lang.Math;

import  android.util.Log;

import com.sct.myopenglapp.R;

public class SquareRenderer implements GLSurfaceView.Renderer
{
    private String TAG="rtsp";

    public SquareRenderer(boolean useTranslucentBackground, Context context)
    {
        // Log.v(TAG, "-----------");
        mTranslucentBackground = useTranslucentBackground;

        this. context  = context;                                   //1
        //   Log.v(TAG, "----------a");
        mSquare = new Square();
        //  Log.v(TAG, "-jjjjjjjjjj");//3
    }

    public void onDrawFrame(GL10 gl)                                         //4
    {

        gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);     //5

        gl.glMatrixMode(GL10.GL_MODELVIEW);                                  //6
        gl.glLoadIdentity();                                                 //7
        //gl.glTranslatef(0.0f, (float) Math.sin(mTransY), -3.0f);               //8

        gl.glTranslatef(0.0f, 0, -1.2f);

        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);                        //9
        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

        //  Log.v(TAG, "---dd----");
        mSquare.draw(gl);                                                    //10
        //   Log.v(TAG, "---dd----");


        mTransY += .075f;
    }

    public void onSurfaceChanged(GL10 gl, int width, int height)        //11
    {
        gl.glViewport(0, 0, width, height);            //12

        float ratio = (float) width / height;
        gl.glMatrixMode(GL10.GL_PROJECTION);                              //13
        gl.glLoadIdentity();
        gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);                       //14
    }

    public void onSurfaceCreated(GL10 gl, EGLConfig config)          //15
    {
        //   Log.v(TAG, "---------aa");
        int resid = R.drawable.flower;            //1
        //    Log.v(TAG, "---------bb");
        mSquare.createTexture(gl, this.context, resid);          //2
        //    Log.v(TAG, "---------cc");

        gl.glDisable(GL10.GL_DITHER);                                        //16

        gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,                       //17
                GL10.GL_FASTEST);

        if (mTranslucentBackground)                                     //18
        {
            gl.glClearColor(0,0,0,0);
        }
        else
        {
            gl.glClearColor(1,1,1,1);
        }
        gl.glEnable(GL10.GL_CULL_FACE);                                     //19
        gl.glShadeModel(GL10.GL_SMOOTH);                                    //20
        gl.glEnable(GL10.GL_DEPTH_TEST);                                    //21
    }
    private boolean mTranslucentBackground;
    private Square mSquare;
    private float mTransY;
    private float mAngle;
    private  Context context;
}




